package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private RedisTemplate redisTemplate;


    @GetMapping("page")
    public R<Page<SetmealDto>> page(Integer page,Integer pageSize,String name){
        Page<SetmealDto> setmealPage = new Page<>(page,pageSize);
        Page<SetmealDto> page1 = setmealService.pageByDto(setmealPage,name);
        return R.success(page1);
    }

    @GetMapping("{id}")
    public R<SetmealDto> setMealGet(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.findByIdWithSetmealAndCategoryName(id);
        return R.success(setmealDto);
    }

    @RequestMapping(method = {RequestMethod.POST,RequestMethod.PUT})
    public R<String> setMealPost(@RequestBody SetmealDto setmealDto){
        Set keys = redisTemplate.keys("setMeal*");
        redisTemplate.delete(keys);
        setmealService.updateSetMelaDto(setmealDto);
        return R.success("添加成功");
    }

    @DeleteMapping
    public R<String> setMealDelete(Long[] ids){
        Set keys = redisTemplate.keys("setMeal*");
        redisTemplate.delete(keys);
        setmealService.deleteIds(ids);
        return R.success("删除成功");
    }

    @PostMapping("status/{status}")
    public R<String> setMealPostStatus(@PathVariable Integer status, Long[] ids){
        Set keys = redisTemplate.keys("setMeal*");
        redisTemplate.delete(keys);
        setmealService.updateSetMelaStatus(status,ids);
        return R.success("修改成功");
    }

    /**
     * 根据分类查询套餐列表
     * @param categoryId
     * @param status
     * @return
     * condition:表示条件存入缓存中，condition属性中是不能使用result结果
     * unless属性：与condition恰好相反
     */
    @Transactional
    @GetMapping("list")
    @Cacheable(cacheNames = "setMeal",key = "#categoryId+':'+#status",unless = "#result.data.size()==0")
    public R<List<Setmeal>> setMealGetList(Long categoryId, Integer status ){

        List<Setmeal> list = setmealService.lambdaQuery()
                .eq(categoryId != null, Setmeal::getCategoryId, categoryId)
                .eq(status != null, Setmeal::getStatus, status)
                .orderByDesc(Setmeal::getUpdateTime)
                .list();
        return R.success(list);
    }

    @GetMapping("dish/{id}")
    public R<List<DishDto>> setMealDishList(@PathVariable Long id){
        String redisKey = "setMeal_"+id;
        List<DishDto> dishDtoList = (List<DishDto>)redisTemplate.opsForValue().get(redisKey);
        if(dishDtoList!=null){
            return R.success(dishDtoList);
        }
        List<DishDto> list = setmealService.getSetMealDishList(id);
        redisTemplate.opsForValue().set(redisKey,list);
        return R.success(list);
    }

}
